
Security News
How Enterprise Security Is Adapting to AI-Accelerated Threats
Socket CTO Ahmad Nassri discusses why supply chain attacks now target developer machines and what AI means for the future of enterprise security.
replace-string
Advanced tools
Replace all substring matches in a string
Similar to String#replace(), but supports replacing multiple matches. You could achieve something similar by putting the string in a RegExp constructor with the global flag and passing it to String#replace(), but you would then have to first escape the string anyways.
$ npm install replace-string
const replaceString = require('replace-string');
const string = 'My friend has a 🐑. I want a 🐑 too!';
replaceString(string, '🐑', '🦄');
//=> 'My friend has a 🦄. I want a 🦄 too!'
Returns a new string with all needle matches replaced with replacement.
Type: string
String to work on.
Type: string
String to match in input.
Type: string | Function
Replacement for needle matches.
If a function, it receives the matched substring, the match count, the original input, and the index in which the match happened (as measured from the original input):
replaceString('Foo 🐑 Bar', '🐑', (matchedSubstring, matchCount, input, matchIndex) => `${matchedSubstring}❤️`);
//=> 'Foo 🐑❤️ Bar'
Type: object
Type: number
Default: 0
Index at which to start replacing.
Type: boolean
Default: false
Whether or not substring matching should be case-insensitive.
RegExp matches in a stringThe string-replace-all package provides similar functionality to replace-string, allowing you to replace all occurrences of a substring within a string. It offers a straightforward API and is useful for basic string replacement tasks.
The replace package is a more versatile tool that can be used for replacing strings in files as well as in strings. It supports regular expressions and offers more advanced replacement options compared to replace-string.
The string.prototype.replaceall package is a polyfill for the String.prototype.replaceAll method, which is a standard method in modern JavaScript. It provides a native way to replace all occurrences of a substring within a string, making it a more standardized option compared to replace-string.
FAQs
Replace all substring matches in a string
The npm package replace-string receives a total of 184,057 weekly downloads. As such, replace-string popularity was classified as popular.
We found that replace-string demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
Socket CTO Ahmad Nassri discusses why supply chain attacks now target developer machines and what AI means for the future of enterprise security.

Security News
Learn the essential steps every developer should take to stay secure on npm and reduce exposure to supply chain attacks.

Security News
Experts push back on new claims about AI-driven ransomware, warning that hype and sponsored research are distorting how the threat is understood.